<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
     <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">
            添加商品
          </el-button>
        </el-col>
      </el-row>
    <!-- 用户列表区 -->
      <!-- 批量录入商品数据，手动输入商品编号，商品名称，商品单价，供应商编号，商品简介，备注 -->
      <!-- goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers -->
      <el-table :data="goodslist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品编号" prop="goodsid"></el-table-column>
        <el-table-column label="商品名称" prop="goodsname"></el-table-column>
        <el-table-column label="商品单价" prop="goodsprice"></el-table-column>
        <el-table-column label="供应商编号" prop="goodssupplierid"></el-table-column>
        <el-table-column label="商品简介" prop="goodsdocuments"></el-table-column>
        <el-table-column label="备注" prop="goodsothers"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip effect="dark" content="修改商品" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goodsid)"></el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-tooltip effect="dark" content="删除商品" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeWorkerByID(scope.row.goodsid)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 10, 20, 30, 40, 50]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </el-card>
    <!-- 添加用户的对话框 -->
    <el-dialog
      title="商品信息"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClosed">
      <!-- 内容主体区 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <!-- 批量录入商品数据，手动输入商品编号，商品名称，商品单价，供应商编号，商品简介，备注 -->
        <!-- goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers -->
        <el-form-item label="商品编号" prop="goodsid">
          <el-input v-model="addForm.goodsid"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input v-model="addForm.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" prop="goodsprice">
          <el-input v-model="addForm.goodsprice"></el-input>
        </el-form-item>
        <el-form-item label="供应商编号" prop="goodssupplierid">
          <el-input v-model="addForm.goodssupplierid"></el-input>
        </el-form-item>
        <el-form-item label="商品简介" prop="goodsdocuments">
          <el-input v-model="addForm.goodsdocuments"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="goodsothers">
          <el-input v-model="addForm.goodsothers"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoods">确 定</el-button>
      </span>
      <!-- 修改用户的对话框 -->
    </el-dialog>
    <el-dialog
      title="修改商品"
      :visible.sync="editDialogVisible"
      width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="80px">
        <!-- 批量录入商品数据，手动输入商品编号，商品名称，商品单价，供应商编号，商品简介，备注 -->
        <!-- goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers -->
        <el-form-item label="商品编号" prop="goodsid">
          <el-input v-model="editForm.goodsid" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input v-model="editForm.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" prop="goodsprice">
          <el-input v-model="editForm.goodsprice"></el-input>
        </el-form-item>
        <el-form-item label="供应商编号" prop="goodssupplierid">
          <el-input v-model="editForm.goodssupplierid"></el-input>
        </el-form-item>
        <el-form-item label="商品简介" prop="goodsdocuments">
          <el-input v-model="editForm.goodsdocuments"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="goodsothers">
          <el-input v-model="editForm.goodsothers"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除用户的对话框 -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取员工列表的参数
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前的页面大小
        pagesize: 10
      },
      goodslist: [],
      total: 0,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      // 这是添加的表单对象
      // goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers
      addForm: {
        goodsid: '',
        goodsname: '',
        goodsprice: '',
        goodssupplierid: '',
        goodsdocuments: '',
        goodsothers: ''
      },
      // 这是编辑用户的表单对象
      editForm: {},
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      // 添加表单规则的对象
      // goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers
      addFormRules: {
        goodsid: [
          { required: true, message: '请输入商品编号', trigger: 'blur' },
          { min: 3, max: 10, message: '商品编号的长度在3~10个字符之间', trigger: 'blur' }
        ],
        goodsname: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 20, message: '商品名称的长度在2~20个字符之间', trigger: 'blur' }
        ],
        goodsprice: [
          { required: true, message: '请输入商品单价', trigger: 'blur' },
          { min: 1, max: 15, message: '商品单价的长度在1~15个字符之间', trigger: 'blur' }
        ],
        goodssupplierid: [
          { required: true, message: '请输入供应商', trigger: 'blur' },
          { min: 3, max: 20, message: '供应商的长度在3-20个字符之间', trigger: 'blur' }
        ],
        goodsdocuments: [
          { required: true, message: '请输入商品简介', trigger: 'blur' },
          { min: 1, max: 200, message: '商品简介的长度为1~200', trigger: 'blur' }
        ],
        goodsothers: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 4, max: 20, message: '备注的长度在4-20个字符之间', trigger: 'blur' }
        ]
      },
      editFormRules: {
        goodsid: [
          { required: true, message: '请输入商品编号', trigger: 'blur' },
          { min: 3, max: 10, message: '商品编号的长度在3~10个字符之间', trigger: 'blur' }
        ],
        goodsname: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 20, message: '商品名称的长度在2~20个字符之间', trigger: 'blur' }
        ],
        goodsprice: [
          { required: true, message: '请输入商品单价', trigger: 'blur' },
          { min: 1, max: 15, message: '商品单价的长度在1~15个字符之间', trigger: 'blur' }
        ],
        goodssupplierid: [
          { required: true, message: '请输入供应商', trigger: 'blur' },
          { min: 3, max: 20, message: '供应商的长度在3-20个字符之间', trigger: 'blur' }
        ],
        goodsdocuments: [
          { required: true, message: '请输入商品简介', trigger: 'blur' },
          { min: 1, max: 200, message: '商品简介的长度为1~200', trigger: 'blur' }
        ],
        goodsothers: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 4, max: 20, message: '备注的长度在4-20个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('/goods', { params: this.queryInfo })
      if (res.code !== 20000) return this.$message.error('获取商品表失败')
      this.goodslist = res.data.value
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听 pagenum 改变的事件
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 监听表单关闭
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 添加新用户
    addGoods () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return valid
        }
        // 添加网络校验请求
        const { data: res } = await this.$http.post('goods', this.addForm)
        if (res.code === 30002) return this.$message.error('该商品已经存在')
        if (res.code !== 20000) return this.$message.error('添加商品失败')
        this.$message.success('添加商品成功')
        this.addDialogVisible = false
        // 从新获取用户列表
        this.getGoodsList()
      })
    },
    // 展示编辑用户的对话框
    async showEditDialog (id) {
      this.queryInfo.query = id
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.code !== 20000) {
        return this.$message.error('查询商品信息失败')
      }
      this.editForm = res.data.value[0]
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户信息并提交
    editGoodsInfo () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return console.log(valid)
        // goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers
        const { data: res } = await this.$http.put('goods', {
          goodsid: this.editForm.goodsid,
          goodsname: this.editForm.goodsname,
          goodsprice: this.editForm.goodsprice,
          goodssupplierid: this.editForm.goodssupplierid,
          goodsdocuments: this.editForm.goodsdocuments,
          goodsothers: this.editForm.goodsothers
        })
        if (res.code !== 20000) return this.$message.error('更新用户信息失败!')
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getGoodsList()
        // 爱上的健康
        this.$message.success('更新用户信息成功')
      })
    },
    // 根据 id 值来删除用户
    async removeWorkerByID (id) {
      // 弹框询问用户是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该供应商, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果点确定返回一个字符串 confirm, 如果取消返回字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      console.log(id)
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.code !== 20000) {
        return this.$message.error('删除用户失败！')
      }
      this.$message.success('删除用户成功！')
      this.getGoodsList()
    }
  }
}
</script>

<style lang="less" scoped>

</style>
